# -*- coding: utf-8 -*-


import unittest
import sys
import os
import time

# 添加执行环境
file_exec_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, file_exec_path)
from framework_strategy.gopy.trader.engine import EmailEngine, MainEngine, EventEngine  # NOQA
from framework_strategy.gopy.base.api import API, Event  # NOQA
from framework_strategy.gopy.gateways.object import *  # NOQA
from framework_strategy.gopy.gateways.xt.v3.spot.xt_api import XTGateway  # NOQA
from framework_strategy.gopy.gateways.xt.v4.future.xt4_future_api import XT4FutureGateway  # NOQA
from framework_strategy.gopy.gateways.binance.binance_api import BINANCEGateway # NOQA


def init_engine():
    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)

    xt_gateway = XT4FutureGateway(main_engine, event_engine, "XT4Future")
    xt_gateway = BINANCEGateway(main_engine, event_engine, "BINANCE")
    # event_engine.start()
    # 获取服务器时间同步获取
    api = API(event_engine)
    return event_engine, api

def init_engine_1():

    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)

    xt4_gateway = XT4FutureGateway(main_engine, event_engine, "XT4Future")
    xt4_gateway.on_bind()
    return event_engine


class TestXT4SpotGateway(unittest.TestCase):

    @unittest.skip
    def test_get_all_market_config(self):
        
        event_engine = init_engine_1()
        api = API(event_engine)
        # 获取服务器时间同步获取
        market_event = MarketsConfigData(_source="XT4FUTURE")
        nt = time.time()
        api.get_all_market_config(market_event)
        res = market_event.get_result(timeout=5)

        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_kline(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketKlineData(_source="XT4FUTURE", type="1m", symbol="btc_usdt")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_klines(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_ticker(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketTickerData(_source="XT4FUTURE",market="btc_usdt")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_ticker(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_tickers(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketTickersData(_source="XT4FUTURE")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_tickers(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_depth(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketDepthData(_source="XT4FUTURE",market="btc_usdt")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_depth(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()       

    @unittest.skip
    def test_get_trades(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketTradesData(_source="XT4FUTURE",market="btc_usdt")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_trades(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_balance(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = AccountBalanceData(
            _source="XT4FUTURE",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a"
            )
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_balance(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()  

    @unittest.skip
    def test_get_fund(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = AccountFundData(
            _source="XT4FUTURE",
            coin="usdt",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a"
            )
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_fund(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()  

    # @unittest.skip
    def test_send_order(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = AccountSendOrderData(
            _source="XT4FUTURE",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a",
            market="btc_usdt",
            price=20100,
            type=0,
            entrustType=0,
            number=1,
            positionSide="LONG",
            _extra={
            "market":"btc_usdt",
            "price":"1000",
            "type":"1",
            "entrustType":"0",
            "number":"0.001",
            "app_name":"test", 
            "user_name":"test", 
            "uuid": 12314213,
            "ccc":"test"} # 需要额外的传参上下文
            )
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.send_order(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop() 

if __name__ == "__main__":
    unittest.main()